package com.lantosec.hmrp.core.kit;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * Result 用于返回值封装
 *
 * @author 杨成宇
 * @version 1.0
 * @date 2019/10/22
 */
@Data
@NoArgsConstructor
@ApiModel(description = "返回响应数据")
public class Result<T> implements Serializable {
    @ApiModelProperty(value = "状态")
    private String state;
    @ApiModelProperty(value = "信息")
    private String message;
    @ApiModelProperty(value = "操作时间")
    private Long timestamp = new Date().getTime();
    @ApiModelProperty(value = "返回数据集合")
    private T data;

    public static Result by(ResultEnum re) {
        return (new Result()).set(re);
    }

    public static <T> Result<T> by(ResultEnum re, T data) {
        return (new Result()).set(re, data);
    }

    public static <T> Result<Page<T>> by(Page<T> page) {
        Result result = new Result();
        result.set(ResultEnum.SUCCESS);
        result.setData(page);
        return result;
    }

    public static Result ok() {
        return (new Result()).setOk();
    }

    public static <T> Result<T> ok(T data) {
        return (new Result()).setOk(null, data);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return (new Result()).setOk(msg, data);
    }

    public static Result fail(String msg) {
        return (new Result()).setFail(msg);
    }

    public static <T> Result<T> fail(String msg, T data) {
        return (new Result()).setFail(msg, data);
    }

    public Result setOk() {
        return this.setOk(null, null);
    }

    public Result setOk(String msg) {
        return this.setOk(msg, null);
    }

    public Result setOk(String msg, T data) {
        this.setState(ResultEnum.SUCCESS.code);
        this.setMessage(msg != null ? msg : ResultEnum.SUCCESS.msg);
        this.setData(data);
        this.setTimestamp(new Date().getTime());
        return this;
    }

    public Result setFail() {
        return setFail(null, null);
    }

    public Result setFail(String msg) {
        return setFail(msg, null);
    }

    public Result setFail(String msg, T data) {
        this.setState(ResultEnum.FAILURE.code);
        this.setMessage(msg != null ? msg : ResultEnum.FAILURE.msg);
        this.setData(data);
        this.setTimestamp(new Date().getTime());
        return this;
    }

    public Result set(ResultEnum re) {
        return set(re, null, null);
    }

    public Result set(ResultEnum re, T data) {
        return set(re, null, data);
    }

    public Result set(ResultEnum re, String msg, T data) {
        this.setState(re.code);
        this.setMessage(msg != null ? msg : re.msg);
        this.setData(data);
        this.setTimestamp(new Date().getTime());
        return this;
    }

    public String toJson() {
        return JSONObject.toJSONString(this);
    }

    public static void main(String[] args) {
        Result r = Result.ok("成功");
        System.out.println(r);
        System.out.println(r.toJson());
    }
}